﻿@using KPIS.GERP.Models.ICM
@using KPIS.GERP.Models
@model IEnumerable<InventoryRepair>

@{
    ViewBag.Title = "รายการที่ส่งซ่อมแซม";
    var service = new KPIS.GERP.WEB.GIMS.RepairService.RepairServiceClient(); 
}

@using (Html.BeginForm("DeleteAll", "Repair", FormMethod.Post ,new { id="form" }))
{

    
    @Html.AntiForgeryToken()
    
    <div class="row-fluid">    
       <p id="message"></p>
        <div class="block-button-menu-above">
            @Html.ButtonAdd(false,"SearchAsset")                
            @Html.ButtonDeleteAll()
        </div>

        <div class = "alert-error">@Html.ValidationSummary()</div>

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
					@ViewBag.Title
                </div>
				@Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width:50px;">
						<!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox"/>
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th>รหัสรายการที่ส่งซ่อม</th>
                    <th>ชื่อรายการที่ส่งซ่อม</th>
                    <th>รายละเอียดที่ส่งซ่อม</th>
                    <th>สาเหตุที่ส่งซ่อม</th>
                    <th>หน่วยงานที่ทำเรื่องส่งซ่อม</th>
                    <th>บริษัทที่ทำเรื่องส่งซ่อม</th>
                    <th>สถานะการส่งซ่อม</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>

            <tbody>
                @foreach ( var m in Model )
                {
                    <tr>
                        <td class="center">
							<!-- Check box all for delete multiple records -->
                            <label>
                                <input type="checkbox" name="deleteIds" value="@m.RepairId" />
                                <span class="lbl"></span>
                            </label>
                        </td>
						<td>@m.RepairId</td>
                        <td>
                            @if(m.AssetCode != null)
                            {
                                var Asset = service.SelectAsset(new Asset()
                                            {
                                                AssetCode = m.AssetCode,
                                                RecordStatus = "!" + KPIS.GERP.Models.SYS.RecordStatus.DeletedStatus
                                            }, null).FirstOrDefault();
                                ViewBag.AssetName = Asset.AssetCode + " - " + Asset.SerialNumber + " - " + Asset.AssetName;
                            }
                            @ViewBag.AssetName
                        </td>
                        <td>@m.RepairDescr</td>
                        <td>@m.Cause</td>
                        <td>@Html.Raw(m.JobSiteId == null ? "-" : m.JobSite.Name)</td>
                        <td>@Html.Raw(m.DealerRepairId == null ? "-" : m.DealerRepair.NameTh)</td>
                        <td>
                            @if (m.MasRepairStatus.Description == "A")
                            {
                                @Html.Raw("<font color='green'>")
                            }
                            else
                            {
                                @Html.Raw("<font color='red'>")
                            }
                            @Html.DisplayFor(model => m.MasRepairStatus.LookUpName)
                            @Html.Raw("</font>")
                        </td>
                        <td>
                            @* เช็คสถานะว่าเป็นสถานะ "กำลังซ่อมแซม"? ถ้าใช่ก็จะแสดงปุ่มแก้ไข แต่ถ้าไม่ก็จะซ่อนปุ่มแก้ไข *@
                            @if ( m.RepairStatus == 70 )
                            {
                                @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                {
                                    Id = m.RepairId,
                                    DetailsUrl = Url.Action("Details") + "/" + m.RepairId,
                                    EditUrl = Url.Action("Edit") + "/" + m.RepairId,
                                    DeleteUrl = Url.Action("Delete") + "/" + m.RepairId
                                })
                            }
                            else
                            {
                                @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                {
                                   Id = m.RepairId,
                                   DetailsUrl = Url.Action("Details") + "/" + m.RepairId,
                                   DeleteUrl = Url.Action("Delete") + "/" + m.RepairId
                                })
                            }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

@section scripts {
	<script type="text/javascript">

		$(function () {
			var oTable1 = $('#table').dataTable({
				"aoColumns": [
                    { "bSortable": false },
                    null, null, null, null, null, null, null,
                    { "bSortable": false }
				]
			});

			$('table th input:checkbox').on('click', function () {
				var that = this;
				$(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});

			});

			$('[data-rel=tooltip]').tooltip();

			$("form").submit(function () {
				var currentForm = this;
				bootbox.confirm(function (result) {
					var _result = result;
					if (result) {
						$.ajax({
							url: '@(Url.Action("DeleteAll", "Repair"))',
							type: "post",
							dataType: "json",
							data: $(currentForm).serialize(),
							success: function (result) {
							    if (result.success) {
							        $("#message").html(result.message);
									location.reload();
								}
							    else {
							        $("#message").html(result.message);
								}
							}
						});
					}
				})
				return false;
			});
		})


	    
	   

	</script>

}